package com.xike.microsysserver.uitl;

import io.swagger.annotations.ApiModelProperty;

import java.util.HashMap;
import java.util.Map;

/**
 * Auther: wuson  <br/>
 * Date: 2021/1/20:17:13  <br/>
 * Description:
 */
public class ResponseEntity<T> {
    private int code;
    private String msg;
    private T object;
    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    private Map<String, Object> data = new HashMap<>();

    public ResponseEntity() {
    }

    public ResponseEntity(T object) {
        code = 200;
        msg = "成功";
        this.object = object;
    }

    @Override
    public String toString () {
        return "ResponseEntity{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", object=" + object +
                ", data=" + data +
                '}';
    }

    public Boolean getSuccess () {
        return success;
    }

    public void setSuccess (Boolean success) {
        this.success = success;
    }

    public int getCode () {
        return code;
    }

    public void setCode (int code) {
        this.code = code;
    }

    public String getMsg () {
        return msg;
    }

    public void setMsg (String msg) {
        this.msg = msg;
    }

    public T getObject () {
        return object;
    }

    public void setObject (T object) {
        this.object = object;
    }

    public Map<String, Object> getData () {
        return data;
    }

    public void setData (Map<String, Object> data) {
        this.data = data;
    }

    //要返回多个数据可调用此方法
    public ResponseEntity data (String key, Object value) {
        code = 200;
        msg = "成功";
        this.data.put(key, value);
        this.setSuccess(true);
        return this;
    }

    public ResponseEntity data (Map<String, Object> map) {
        code = 200;
        msg = "成功";
        this.data = map;
        this.setSuccess(true);
        return this;
    }

    public static ResponseEntity error () {
        ResponseEntity r = new ResponseEntity();
        r.setCode(400);
        r.setMsg("失败");
        r.setSuccess(false);
        return r;
    }
    public static ResponseEntity error (String msg) {
        ResponseEntity r = new ResponseEntity();
        r.setCode(400);
        r.setMsg(msg);
        r.setSuccess(false);
        return r;
    }

    //成功静态方法
    public static ResponseEntity ok () {
        ResponseEntity r = new ResponseEntity();
        r.setCode(200);
        r.setSuccess(true);
        r.setMsg("成功");
        return r;
    }

}
